#include("header.html")
<div class="container">
    <div class="box box-info">
        <div class="box-header">
            <h4 class="box-title"><span class="fa fa-pencil-square-o"></span>信息修改</h4>
        </div>
        <div class="box-body">

            <form method="post" class="form-horizontal">
                <input type="hidden" name="id" value="${user.id ?! ''}">
                <div class="form-group">
                    <div  id="error" style="color: red;margin-left: 40%;margin-bottom: 2%;display: none;"></div>
                    <label for="real_name" class="control-label col-lg-2 col-sm-2 col-xs-4">姓名</label>
                    <div class="col-lg-10 col-sm-10">
                        <input name="realName" id="real_name" class="form-control" placeholder="${user.realName ?! ''}"
                               onfocus="errorNone();">
                    </div>
                </div>
                <div class="form-group">
                    <label for="tel" class="control-label col-lg-2 col-sm-2 col-xs-4">手机号</label>
                    <div class="col-lg-10 col-sm-10">
                        <input name="tel" id="tel" class="form-control" placeholder="${user.tel ?! ''}" maxlength="11" onfocus="errorNone();">
                    </div>
                </div>
                <div class="form-group">
                    <label for="email" class="control-label col-lg-2 col-sm-2 col-xs-4">邮箱</label>
                    <div class="col-lg-10 col-sm-10">
                        <input name="email" id="email" class="form-control" placeholder="${user.email ?! ''}" onfocus="errorNone();">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-12" style="margin-top: 5px;">
                        <button type="submit" class="btn btn-info btn-block">确认修改</button>
                    </div>
                </div>
            </form>
            #include("sign.html")
        </div>
    </div>
</div>

<script>
    $('form').submit(function (e) {
        e.preventDefault();
        var phone = checkPhone();
        var email = checkEmail();
        var name = checkName();
        var flag = checkAllNone();
        if(flag==1&&(email==1||phone==1||name==1)){
            document.getElementById('error').style.display='none';
            $.post('/user/change', $(this).serialize(), function (data) {
                if (data.success) {
                    alert('SUCCESS: 修改成功！');
                    location.href = '/user/home'
                } else {
                    alert('ERROR: ' + data.msg);
                }
            });
        }
//        var name = $('#real_name').val().trim();
//        var email = $('#email').val().trim();
//        var tel = $('#tel').val().trim();
//        if (name === "" && email === "" && tel === "") {
//            console.log("没做任何修改，不能提交！");
//            return;
//        }
//        if (tel !== "" && !/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(tel)) {
//            alert("电话号码格式错误！");
//            return;
//        }



    });
    function checkAllNone() {
        if($('#real_name').val()==''&&$('#tel').val()==''&&$('#email').val()==''){
            document.getElementById('error').innerText="需要修改的信息不能全为空！";
            document.getElementById('error').style.display='block';
            return 0;
        }
        else {
            return 1;
        }
    }
    function checkName() {
        if($('#real_name').val()=='') return 0;
        else return 1;
    }
    function checkPhone() {
        var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
        if($('#tel').val()=='') return 0;
        if (!myreg.test($('#tel').val())) {
            document.getElementById('error').innerText="手机号输入不正确！";
            document.getElementById('error').style.display='block';
            return 0;
        } else {
            return 1;
        }
    }
    function checkEmail(){
        if($('#email').val()=='') return 0;
        var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //正则表达式
        var obj = document.getElementById("email"); //要验证的对象
        if(!reg.test(obj.value)){ //正则验证不通过，格式不对
            document.getElementById('error').innerText="邮箱输入不正确！";
            document.getElementById('error').style.display='block';
            return 0;
        }else{
            return 1;
        }
    }
    function errorNone(){
        document.getElementById('error').style.display='none';
    }
</script>

</body>
</html>